package com.dalongxin.work.page.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.XRefreshViewFooter;
import com.dalongxin.work.R;
import com.dalongxin.work.data.IndexHomeData;
import com.dalongxin.work.page.CloudDiskActivity;
import com.dalongxin.work.page.DjBuyActivity;
import com.dalongxin.work.page.DjPayActivity;
import com.dalongxin.work.page.DjFeeActivity;
import com.dalongxin.work.page.DjHtpsActivity;
import com.dalongxin.work.page.DjHtspActivity;
import com.dalongxin.work.page.adapter.IndexHomeAdapter;
import com.dalongxin.work.utils.ItemClickSupport;
import com.dalongxin.work.utils.StatusBarUtil;
import com.dalongxin.work.utils.Utils;
import com.dalongxin.work.view.CustomFooterView;
import com.dalongxin.work.view.SmileyHeaderView;

import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.fragment_main_home)
public class HomeFragment extends BaseFragment {

    @ViewInject(R.id.recycler_view)
    private RecyclerView recyclerView;
    @ViewInject(R.id.xrefreshview)
    private XRefreshView xRefreshView;

    private IndexHomeAdapter adapter;
    private List<IndexHomeData> indexHomeDataList;

    private View rootView;
    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.e("--HomeFragment--onCreate--");
        indexHomeDataList = new ArrayList<>();
        adapter = new IndexHomeAdapter(getActivity(), indexHomeDataList);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(rootView == null) {
            rootView = x.view().inject(this, inflater, container);
        }
        StatusBarUtil.setPaddingSmart(getActivity(), xRefreshView);
        //有图片 往上提
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)rootView.getLayoutParams();
        layoutParams.topMargin = Integer.valueOf("-" + Utils.getStatusBarHeight(getActivity()));
        rootView.setLayoutParams(layoutParams);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        // 静默加载模式不能设置footerview
        recyclerView.setAdapter(adapter);
        //设置刷新完成以后，headerview固定的时间
        xRefreshView.setPinnedTime(1000);
        xRefreshView.setMoveForHorizontal(true);
        //xRefreshView1.setAutoLoadMore(true);
        adapter.setCustomLoadMoreView(new XRefreshViewFooter(getActivity()));
        //设置静默加载时提前加载的item个数
        //xRefreshView1.setPreLoadCount(4);
        xRefreshView.setCustomHeaderView(new SmileyHeaderView(getActivity()));
        xRefreshView.setCustomFooterView(new CustomFooterView(getActivity()));
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh(boolean isPullDown) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initIndexHomeData();
                        adapter.notifyDataSetChanged();
                        xRefreshView.stopRefresh();
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(boolean isSilence) {

            }
        });
        ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, View itemView, int position) {
                Intent intent;
                switch (position) {
                    case 1:
                        intent = new Intent(getActivity(), CloudDiskActivity.class);
                        startActivity(intent);
                        break;
                    case 2:
                        intent = new Intent(getActivity(), DjHtspActivity.class);
                        startActivity(intent);
                        break;
                    case 3:
                        intent = new Intent(getActivity(), DjBuyActivity.class);
                        startActivity(intent);
                        break;
                    case 4:
                        intent = new Intent(getActivity(), DjFeeActivity.class);
                        startActivity(intent);
                        break;
                    case 5:
                        intent = new Intent(getActivity(), DjPayActivity.class);
                        startActivity(intent);
                        break;
                    case 6:
                        intent = new Intent(getActivity(), DjHtpsActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        });
        xRefreshView.setPullRefreshEnable(true);//允许下拉刷新
        xRefreshView.setPullLoadEnable(false);//禁止加载更多
        xRefreshView.setAutoRefresh(true);
        return rootView;
    }

    private void initIndexHomeData() {
        //0轮播图 1档案管理 2合同审批 3采购计划 4费用报销 5付款申请 6合同评审
        indexHomeDataList.clear();
        IndexHomeData home0 = new IndexHomeData();
        home0.setType(0);
        home0.setResId(R.mipmap.default_img_16_9);

        IndexHomeData home1 = new IndexHomeData();
        home1.setType(1);
        home1.setTitle("档案管理");
        home1.setResId(R.mipmap.index_home_icon);

        IndexHomeData home2 = new IndexHomeData();
        home2.setType(1);
        home2.setTitle("合同审批");
        home2.setResId(R.mipmap.index_home_icon);

        IndexHomeData home3 = new IndexHomeData();
        home3.setType(1);
        home3.setTitle("采购计划");
        home3.setResId(R.mipmap.index_home_icon);

        IndexHomeData home4 = new IndexHomeData();
        home4.setType(1);
        home4.setTitle("费用报销");
        home4.setResId(R.mipmap.index_home_icon);

        IndexHomeData home5 = new IndexHomeData();
        home5.setType(1);
        home5.setTitle("付款申请");
        home5.setResId(R.mipmap.index_home_icon);

        IndexHomeData home6 = new IndexHomeData();
        home6.setType(1);
        home6.setTitle("合同评审");
        home6.setResId(R.mipmap.index_home_icon);

        indexHomeDataList.add(home0);
        indexHomeDataList.add(home1);
        indexHomeDataList.add(home2);
        indexHomeDataList.add(home3);
        indexHomeDataList.add(home4);
        indexHomeDataList.add(home5);
        indexHomeDataList.add(home6);
    }
}
